import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author lumi
 * @date 2020/1/16 1:13
 */

public class Consumer_subcribe_sms {
    private static final String QUEUE  = "helloworld";
    private static final String QUEUE_INFORM_SMS = "QUEUE_INFORM_SMS";
    private static final String EXCHANGE_FANOUT_INFORM = "EXCHANGE_FANOUT_INFORM";
    public static void main(String[] args) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);
        channel.exchangeDeclare(EXCHANGE_FANOUT_INFORM,BuiltinExchangeType.FANOUT);
        channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_FANOUT_INFORM,"");
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String exchange = envelope.getExchange();
                long deliveryTag = envelope.getDeliveryTag();
                String msg = new String(body, "utf-8");
                System.out.println("receive msg "+ msg);

            }
        };
        channel.basicConsume(QUEUE_INFORM_SMS,true,defaultConsumer);

    }
}
